<template>
	<view class="mainpadding2" v-if="pageShow">
		<view class="mainpadding_top ffffff radius flexbetween">
			<view class="titletext bold hei_text">物流名稱</view>
			<input class="main_size bold hei_text textright" type="text" placeholder="請輸入物流名稱" v-model="name">
		</view>
		<view class="mainpadding_top ffffff radius">
			<view class="titletext bold hei_text">運費類型</view>
			<view class="margin_top">
				<u-radio-group v-model="type" placement="row" circle>
					<u-radio activeColor="#ff8505" :customStyle="{marginRight: '15rpx'}" label="固定運費"
						name="0"></u-radio>
					<u-radio activeColor="#ff8505" :customStyle="{marginRight: '15rpx'}" label="免運費" name="1"></u-radio>
					<u-radio activeColor="#ff8505" label="免運費折扣" name="2"></u-radio>
				</u-radio-group>
			</view>
		</view>
		<!-- 固定運费 -->
		<template v-if="type==0">
			<view class="mainpadding_top ffffff radius flexbetween">
				<view class="titletext bold hei_text">運費點數</view>
				<input class="main_size bold hei_text textright" type="number" placeholder="請輸入運費點數" v-model="money_g">
			</view>
		</template>
		<!-- 免運费折扣 -->
		<template v-if="type==2">
			<view class="mainpadding_top ffffff radius">
				<view class="titletext bold hei_text">運費點數</view>
				<view class="flexleft margin_top">
					<view class="main_size  bold hei_text margin_right">固定運費</view>
					<view class="flexleft iteminputjs1">
						<view class="huang_text main_size bold margin_right">$</view>
						<input class="main_size bold huang_text" type="number" placeholder="" v-model="money_g">
					</view>
				</view>
				<view class="flexleft margin_top">
					<view class="main_size  bold hei_text margin_right">訂單超過</view>
					<view class="flexleft iteminputjs margin_right">
						<view class="huang_text main_size bold margin_right">$</view>
						<input class="main_size bold huang_text" type="number" placeholder="" v-model="money_count">
					</view>
					<view class="main_size  bold hei_text margin_right">=</view>
					<view class="flexleft iteminputjs">
						<view class="huang_text main_size bold margin_right">$</view>
						<input class="main_size bold huang_text" type="number" placeholder="" v-model="money_jian">
					</view>
				</view>
			</view>
		</template>
		<view class="mainpadding posibottom" style="bottom: 0;">
			<view class="submitbtn" @click="submit">確認</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				shop_id: uni.getStorageSync("shop_id"),
				name: "", //物流名称
				type: "0", //类型:0=固定運费,1=免運费,2=免運费折扣
				money_g: "", //固定運费金额
				money_count: "", //订單满
				money_jian: "", //订單减
				pageShow: false,
				issubmit:false
			}
		},
		onLoad(options) {
			if (options.id) {
				this.id = options.id
				this.getInfo()
			} else {
				this.pageShow = true
			}
		},
		methods: {
			async getInfo() {
				let res = await this.http.request('/api/shop/Freightdeail', 'POST', {
					shop_id: this.shop_id,
					id: this.id
				})
				if (res.code == 1) {
					this.name = res.data.name
					this.type = res.data.type
					this.money_g = res.data.money_g
					this.money_count = res.data.money_count
					this.money_jian = res.data.money_jian
					this.pageShow = true
				} else if (res.code == 0) {
					// this.http.toast(res.msg)
				} else {
					this.http.toast('系統處理中，請稍後再試');
				}
			},
			async submit() { //確認
				let _this = this
				let data = {
						id: this.id, //物流id
						shop_id: this.shop_id,
						name: this.name, //物流名称
						type: this.type, //类型:0=固定運费,1=免運费,2=免運费折扣
						money_g: this.money_g, //固定運费金额
						money_count: this.money_count, //订單满
						money_jian: this.money_jian, //订單减
					},
					url = "",
					text = "";
				if (this.id) {
					url = "/api/shop/editFreight"
					text = "修改成功！"
				} else {
					delete data.id
					url = "/api/shop/addFreight"
					text = "添加成功！"
				}
				if(this.issubmit) return
				this.issubmit = true
				let res = await this.http.request(url, 'POST', data)
				if (res.code == 1) {
					this.http.toast(text)
					setTimeout(() => {
						uni.navigateBack(1)
					}, 500)
				} else {
					this.issubmit = false
					this.http.toast(res.msg)
				}
			}
		}
	}
</script>
<style lang="scss" scoped>
	.iteminputjs {
		width: 200rpx;
		max-width: 200rpx;
		min-width: 200rpx;
		border-radius: 10rpx;
		border: 1rpx solid rgba(0, 0, 0, .1);
		padding: 20rpx 30rpx;
	}

	.iteminputjs1 {
		width: 50%;
		border-radius: 10rpx;
		border: 1rpx solid rgba(0, 0, 0, .1);
		padding: 20rpx 30rpx;
	}
</style>